package com.tongcao.cn.algorithm.designMoel.obsever;

/**
 * @Author:tongcao
 * @Date:2019/4/11 15:48
 * Description:
 */
public class ObserverActivity implements Runnable {


    private MockUser1 mockUser1;

    private MockUser2 mockUser2;

    private ObserverFor3D observerFor3D;

    public void start() {
        //创建服务号
        observerFor3D = new ObserverFor3D();

        //创建两个订阅者
        mockUser1 = new MockUser1(observerFor3D);
        mockUser2 = new MockUser2(observerFor3D);

        //发送信息
        observerFor3D.setMsg("20190411中奖号码" + System.currentTimeMillis());
        observerFor3D.setMsg("20190412中奖号码" + System.currentTimeMillis());

    }


    public static void main(String[] args) {
        new Thread(new ObserverActivity()).start();
    }


    /**
     * 关闭活动
     */
    public void stop() {
        if (mockUser1 != null) {
            observerFor3D.removeObServer(mockUser1);
        }
        if (mockUser2 != null) {
            observerFor3D.removeObServer(mockUser2);
        }
        observerFor3D = null;
    }

    @Override
    public void run() {
        start();
        stop();
    }
}